package com.mask.roll.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mask.roll.edu.client.VodClient;
import com.mask.roll.edu.entity.Video;
import com.mask.roll.edu.mapper.VideoMapper;
import com.mask.roll.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author hx
 * @since 2021-10-23
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;
    @Override
    public void removeVideoByCourseId(String courseId) {

        //根据课程id查询所有视频id
        QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id",courseId);
        //只查询视频id字段
        videoWrapper.select("video_source_id");
        List<Video> videoList = baseMapper.selectList(videoWrapper);

        ArrayList<String> videoIds = new ArrayList<>();
        for (int i = 0; i < videoList.size(); i++) {
            String videoSourceId = videoList.get(i).getVideoSourceId();
            if (!StringUtils.isEmpty(videoSourceId)) {
                videoIds.add(videoSourceId);
            }
        }
        if (videoIds.size() > 0) {
            //删除多个视频
            vodClient.deleteVodBatch(videoIds);
        }

        //删除小节
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
